#Locking down violence 10.18.22
#Replication file 
#Figures 3 and 4

####################################################################################
#Packages
#Packages (including packages for use in the mapping)
library(rayshader)
library(ggplot2)
library(tidyverse)
library(sf)

####################################################################################
rm(list=ls()) #Clearing workspace
####################################################################################

load(file= "ne_iraq_gov.shp", verbose = FALSE)

#Figure 3

load(file= "no.tban.map.Rdata", verbose = FALSE)


#Before travel ban
ggplot() +
  geom_sf(data = ne_iraq_gov,show.legend=FALSE)+ #layers country outline
  geom_sf(data =no.tban.map, aes(fill=N.Events)) +
  scale_fill_gradient( low = "#ace5ee",
                       high = "red", limits=c(0,100))+  #change to respond to R&R 12.14.2020
  coord_sf(xlim = c(38.8, 48.5), ylim = c(29, 37.5), expand = FALSE)+ #zooms in on the map
  theme_bw()+ #eliminates fills
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank())+ #eliminates various axis ticks and legends
  theme(legend.title = element_text(size=12))+ #specifies legend text size
  theme(legend.position="left") # positions the legend with respect to the map

plot_gg(gg_iraq_beforeban, multicore = FALSE, width = 5, height=4, fov = 10)

#During travelban for all

load(file= "tban.map.Rdata", verbose = FALSE)

ggplot() +
  geom_sf(data = ne_iraq_gov,show.legend=FALSE)+ #layers country outline
  geom_sf(data =tban.map, aes(fill=N.Events)) +
  scale_fill_gradient( low = "#ace5ee",
                       high = "red", limits=c(0,100))+ 
  coord_sf(xlim = c(38.8, 48.5), ylim = c(29, 37.5), expand = FALSE)+ #zooms in on the map
  theme_bw()+ #eliminates fills
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank())+ #eliminates various axis ticks and legends
  theme(legend.title = element_text(size=12))+ #specifies legend text size
  theme(legend.position="left") # positions the legend with respect to the map

plot_gg(gg_iraq_tban, multicore = FALSE, width = 5, height=4, fov = 10)


####################################################################################
#Figure 4

#Travelban Chisquare test
load(file= "tban.chi.Rdata", verbose = FALSE)
chisq.test(tban.chi, simulate.p.value = TRUE)

#Travelban Chisquare plot 
load(file= "tban.chi.plot.Rdata", verbose = FALSE)

ggplot(data=tban.chi.plot, aes(x=Governorate, y=Events, fill=time, color)) +
  geom_bar(stat="identity", position=position_dodge())+
  theme_bw()+
  scale_fill_manual(values=c("#ace5ee", "red"))+
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.ticks.y=element_blank(),
        axis.ticks.x=element_blank(),
        axis.text.x = element_text(angle = 70,hjust=.9,family="Times New Roman", size = 11),
        axis.text.y = element_text(family="Times New Roman", size = 11),
        axis.title.x = element_text(family="Times New Roman", size = 12),
        axis.title.y = element_text(family="Times New Roman", size = 12))+
  annotate(geom="text", x=1.5, y=100, label="Chi-square",
           color="black", angle=0, size=4, fontface="bold",family="Times New Roman")+ #adds text on xy coordinates
  annotate(geom="text", x=1.5, y=94, label="p = 0.0004",
           color="black", angle=0, size=4, fontface="bold",family="Times New Roman") #adds text on xy coordinates

